package com.qf.electronic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.electronic.dto.RoleDto;
import com.qf.electronic.dto.RoleStateDto;
import com.qf.electronic.pojo.Role;
import com.qf.electronic.mapper.RoleMapper;
import com.qf.electronic.service.IRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wu
 * @since 2023-05-04
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

    @Transactional(readOnly = true, isolation = Isolation.READ_COMMITTED)
    @Override
    public Page<Role> searchRoles(RoleDto roleDto) {
        Page<Role> page = new Page<>(roleDto.getCurrentPage(), roleDto.getPageSize());
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        String name = roleDto.getName();
        if(StringUtils.hasLength(name)){
            wrapper.like("name", name);
        }
        Integer enableStatus = roleDto.getEnableStatus();
        if(enableStatus != null){
            wrapper.eq("enable_status", enableStatus);
        }
        return page(page, wrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean updateRoleState(RoleStateDto stateDto) {
        UpdateWrapper<Role> wrapper = new UpdateWrapper<>();
        wrapper.set("enable_status",  stateDto.getState());
        wrapper.in("id", stateDto.getRoleIds());
        return update(wrapper);
    }
}
